﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using FarPoint.Web.Spread;

namespace ControlExplorer
{
  public partial class Spread : System.Web.UI.MasterPage
  {
    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);

      if (!IsPostBack)
      {
        //BindThemes();
        themeswitcher.Visible = false;
      }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      BindNavigation();
    }

    private void BindThemes()
    {
      SheetSkin[] skins = DefaultSkins.Skins;
      for (int i = 0; i < skins.Length; i++)
      {
        themeswitcher.Items.Add(new System.Web.UI.WebControls.ListItem(skins[i].Name, skins[i].Name));
      }
    }

    private void BindNavigation()
    {
      System.Xml.Linq.XElement xEle;

      var relativePaths = Request.AppRelativeCurrentExecutionFilePath.Split('/');

      string sample = relativePaths.Length > 3 ? relativePaths[2].ToLower() : string.Empty;
      string pageName = relativePaths.Length > 3 ? relativePaths[3].Replace(".aspx", string.Empty) : string.Empty;

      bool isSample = sample.Equals("groupware") || sample.Equals("orderentry") || sample.Equals("mvcmusicstore");
      if (isSample)
      {
        if (Cache["UseCaseList"] == null)
        {
          xEle = System.Xml.Linq.XElement.Load(Server.MapPath("~/UseCaseList.xml"));
          Cache["UseCaseList"] = xEle;
        }
        else
        {
          xEle = (System.Xml.Linq.XElement)Cache["UseCaseList"];
        }
      }
      else
      {
        if (Cache["ControlList"] == null)
        {
          xEle = System.Xml.Linq.XElement.Load(Server.MapPath("~/ControlList.xml"));
          Cache["ControlList"] = xEle;
        }
        else
        {
          xEle = (System.Xml.Linq.XElement)Cache["ControlList"];
        }
      }
      IEnumerable<XElement> catalogs = from c in xEle.Elements("Control") select c;


      IEnumerable<XElement> pageList = from control2 in catalogs
                                       where control2.Attribute("name").Value.Replace(" ", string.Empty).ToLower() == sample
                                       select control2;

      bool found = false;
      string pageLabel = sample;
      if (pageList != null)
      {
        if (pageList.Count() < 1)
        {
          var childControls = from c in xEle.Descendants("ChildControl") select c;

          pageList = from control2 in childControls
                     where control2.Attribute("name").Value.ToLower() == relativePaths[1].Replace(" ", string.Empty).ToLower()
                     select control2;
        }
        if (pageList.Count() > 0)
        {

          this.navigation.Visible = true;
          this.navigation.Controls.Clear();

          HtmlGenericControl ulSideMenu = new HtmlGenericControl("ul");
          ulSideMenu.Attributes["class"] = "sample-list";
          navigation.Controls.Add(ulSideMenu);

          pageLabel = pageList.First().Attribute("name").Value;
          foreach (XElement action in pageList.First().Elements("action"))
          {
            HtmlGenericControl li = new HtmlGenericControl("li");

            ulSideMenu.Controls.Add(li);
            HtmlGenericControl link = new HtmlGenericControl("a");
            link.InnerHtml = action.Attribute("text").Value;
            link.Attributes["href"] = ResolveClientUrl(action.Attribute("page").Value);
            //link.Attributes["href"] = action.Attribute("page").Value.Substring(2);
            li.Controls.Add(link);


            if (action.Attribute("page").Value.ToLower() == Request.AppRelativeCurrentExecutionFilePath.ToLower())
            {
              li.Attributes["class"] = "demo-config-on";
              Page.Title = action.Parent.Attribute("text").Value + " - " + action.Attribute("text").Value;
              WidgetNameLebel.Text = action.Parent.Attribute("text").Value;
              WidgetSampleLebel.Text = action.Attribute("text").Value;
              //Build documentation link
              XAttribute attr = action.Attribute("docName");
              if (attr == null)
                docs.Visible = false;
              else
              {
                docs.Visible = true;
                var docPath = attr.Value;
                docs.HRef = docPath;
              }
              found = true;
            }
          }
        }
      }
      else
      {
        this.navigation.Visible = false;
      }

      if (!found && isSample)
      {
        WidgetNameLebel.Text = pageLabel;
        Page.Title = pageLabel + " - " + pageName;
        WidgetSampleLebel.Visible = false;
      }
      if (isSample)
      {
        docs.Visible = false;
        //WidgetTabs.Pages[1].Visible = false;
      }
    }
  }
}